import Router from 'koa-router'
import Cart from '../dbs/models/cart.js'
//加密操作
import md5 from 'crypto-js/md5'

let router = new Router({
  prefix: '/cart'
})

router.post('/create', async ctx => {
  //验证登录
  if (!ctx.isAuthenticated()) {
    ctx.body = {
      code: -1,
      msg: 'please login'
    }
  } else {
    let time = Date()
    //需要对购物车的编号进行加密
    let cartNo = md5(Math.random() * 1000 + time).toString()
    let {
      params: {
        id,
        detail
      }
    } = ctx.request.body //注意是post获取参数
    let cart = new Cart({
      id,
      cartNo,
      time,
      user: ctx.session.passport.user,
      detail
    })
    //存入数据库操作
    let result = await cart.save()
    //要响应的信息
    if (result) {
      ctx.body = {
        code: 0,
        msg: '',
        id: cartNo
      }
    } else {
      ctx.body = {
        code: -1,
        msg: 'fail'
      }
    }
  }
})

router.post('/getCart', async ctx => {
  let {
    id
  } = ctx.request.body
  try {
    //查询数据库第一条信息，是因为买一个就创建一个购物车
    let result = await Cart.findOne({
      cartNo: id
    })
    //响应额数据
    ctx.body = {
      code: 0,
      data: result ? result.detail[0] : {}
    }
  } catch (e) {
    console.log(e)
    ctx.body = {
      code: -1,
      data: {}
    }
  }
})

export default router
